{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c66bf94a",
   "metadata": {},
   "source": [
    "# 人脸识别实践——宿舍智能识别系统2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a54503a9",
   "metadata": {},
   "source": [
    "## 创建FaceSet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "31bcc677",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "13f1a372",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "462fafa8",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "FaceSet_Create_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'yUu246tUwqp-rJNefpXo_riIBkQThyeA'\n",
    "API_sercret = 'yqJ_jCT4dbXEjX4Px6QjmxEsZYhQWQpd'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "}\n",
    "r = requests.post(url = FaceSet_Create_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "b4f7b12c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '95b4f5080a88ee65016c2b59889aafae',\n",
       " 'time_used': 160,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648470821,cd3c020c-fffe-42cd-8a91-92f8f216025d',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "7fd14f3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "FaceSet_getfacesets_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "# 2. api账户/通行证\n",
    "API_key = 'yUu246tUwqp-rJNefpXo_riIBkQThyeA'\n",
    "API_sercret = 'yqJ_jCT4dbXEjX4Px6QjmxEsZYhQWQpd'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "}\n",
    "r = requests.post(url = FaceSet_getfacesets_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "49349094",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 87,\n",
       " 'facesets': [{'faceset_token': '162cc13004f44da40fbea2aab6b9b6e0',\n",
       "   'outer_id': 'NFUSD00',\n",
       "   'display_name': '广州南方学院0栋学生宿舍楼',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '44d14fb62073a75d9cf67256d18b582d',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '95b4f5080a88ee65016c2b59889aafae',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648470823,19d5ebdd-ef29-46bb-8ef2-ae738a835375'}"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "e1faff73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'162cc13004f44da40fbea2aab6b9b6e0'"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token=r.json()['facesets'][0]['faceset_token']\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "2eca974a",
   "metadata": {},
   "outputs": [],
   "source": [
    "FaceSet_update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "# 2. api账户/通行证\n",
    "API_key = 'yUu246tUwqp-rJNefpXo_riIBkQThyeA'\n",
    "API_sercret = 'yqJ_jCT4dbXEjX4Px6QjmxEsZYhQWQpd'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'faceset_token':faceset_token,\n",
    "    'display_name':'广州南方学院0栋学生宿舍楼',\n",
    "    'new_outer_id':'NFUSD00',\n",
    "    'user_date':'6层，满员，女生宿舍，460位同学，均已录入face_token'\n",
    "}\n",
    "r = requests.post(url = FaceSet_update_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9df75db",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "33e26992",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 76,\n",
       " 'error_message': 'NEW_OUTER_ID_EXIST',\n",
       " 'request_id': '1648470827,71fa0aba-5466-4f90-b425-91761b1675a2'}"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba861ea7",
   "metadata": {},
   "source": [
    "## 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d4521c38",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "13b99b25",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'yUu246tUwqp-rJNefpXo_riIBkQThyeA'\n",
    "API_sercret = 'yqJ_jCT4dbXEjX4Px6QjmxEsZYhQWQpd'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "08dfb8bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module detect:\n",
      "\n",
      "NAME\n",
      "    detect\n",
      "\n",
      "FUNCTIONS\n",
      "    face_detect(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\枝枝糖~\\documents\\api\\week4\\detect.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "12b0c86b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1649050195,a3008bb1-b88b-4a13-9583-fed6e5365d6c',\n",
       " 'time_used': 90,\n",
       " 'faces': [{'face_token': '014dad752b43629a99957c5828fbac39',\n",
       "   'face_rectangle': {'top': 98, 'left': 164, 'width': 118, 'height': 118}}],\n",
       " 'image_id': 'fwD9nHfTb9s3JIGwQOQmSA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_key,API_sercret,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "83fdc890",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'API_KEY' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-0b07ba9b510e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mdetect\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mimage_url\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'Liu_01.jpeg'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mdetect\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mface_detect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mAPI_KEY\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mAPI_S\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mimage_url\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'API_KEY' is not defined"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c516535b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
